#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkReply>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setStyleSheet(R"(background-color: #f0f0f0;border: 1px solid #ccc;)");
    //调整主窗口
    this->setWindowTitle("Network Disk");
    this->setCentralWidget(&tableWidget);
    this->setMaximumWidth(2000);
    this->resize(1600,1000);
    //初始化成员
    manager=new QNetworkAccessManager(this);
    Refresh=new QPushButton(this);
    Refresh->setText("刷新");
    connect(Refresh,&QPushButton::clicked,this,&MainWindow::listShow);
    upload= new uploadbutton(base_url,this);

    tableWidget.setStyleSheet(R"(background-color: #ffffff;border: 1px solid #aaa;  )");
    tableWidget.insertRow(1);
    tableWidget.setCellWidget(0,1,Refresh);
    tableWidget.setCellWidget(0,2,upload);

    tableWidget.show();
    listShow();
}

MainWindow::~MainWindow()
{
    delete ui;
}

struct fileInfo
{
    QString filename;
    QString fsize;
    QString mtime;
};



void MainWindow::handerShow()
{
    if(response->error()!=QNetworkReply::NoError)
    {
        qDebug()<<"请求文件信息失败";
        return;
    }
    tableWidget.hide();

    QString st = response->readAll(); // 获取服务端发过来的序列化文件信息
    // 使用QJsonDocument解析JSON字符串
    QJsonDocument jsonDocument = QJsonDocument::fromJson(st.toUtf8());
    QJsonArray root = jsonDocument.array(); // 获取json数组

    // 清空表格
    Refresh->deleteLater();//销毁后再次创建
    upload->deleteLater();
    for(int i =tableWidget.rowCount() - 1;i >= 0; i--) tableWidget.removeRow(i);


    // 设置表头
    QStringList headers;
    headers << "文件名" << "大小" << "修改时间"<<"下载";
    tableWidget.setColumnCount(headers.count());
    tableWidget.setHorizontalHeaderLabels(headers);
    int row = 0;
    for (const auto &item : root)
    {
        QJsonObject obj = item.toObject();
        fileInfo fileText;
        fileText.filename = obj["filename"].toString();
        fileText.mtime = obj["last_mtime"].toString();
        fileText.fsize = obj["size"].toString();
        // 插入数据到表格
        tableWidget.insertRow(row);
        tableWidget.setItem(row, 0, new QTableWidgetItem(fileText.filename));
        tableWidget.setItem(row, 1, new QTableWidgetItem(fileText.fsize));
        tableWidget.setItem(row, 2, new QTableWidgetItem(fileText.mtime));

        DownLoadButton* downLoadButton=new DownLoadButton(base_url,fileText.filename,Refresh);//挂载在刷新按钮上,每次刷新都销毁刷新按钮
        tableWidget.setCellWidget(row,3,downLoadButton);//添加下载按钮
        row++;
    }


    Refresh=new QPushButton(this);
    upload= new uploadbutton(base_url,this);


    //连接刷新槽函数
    Refresh->setText("刷新");
    connect(Refresh,&QPushButton::clicked,this,&MainWindow::listShow);
    tableWidget.insertRow(row);//添加多一行用来放置刷新按钮
    tableWidget.setCellWidget(row,1,Refresh);
    tableWidget.setCellWidget(row,2,upload);

    tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//跟随父窗口大小自动调整宽度
    // 显示表格
    tableWidget.show();
    // 释放response
    response->deleteLater();
}

void MainWindow::listShow()
{
    // 构造请求对象
    QUrl url(base_url + "clientShow");
    QNetworkRequest request(url); // 刷新页面请求
    // 发送请求
    response = manager->get(request);
    // 连接槽函数,处理响应
    connect(response, &QNetworkReply::finished, this,&MainWindow::handerShow);
}

